package com.yami.shop.rights.common.listener;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.yami.shop.bean.event.PaySuccessOrderEvent;
import com.yami.shop.bean.model.OrderItem;
import com.yami.shop.bean.order.PaySuccessOrderOrder;
import com.yami.shop.rights.common.strategy.PersonRightsFactory;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Slf4j
@Component("PresellVerificationRightsListener")
@AllArgsConstructor
public class PresellVerificationRightsListener {

    @Autowired
    private PersonRightsFactory personRightsFactory;

    /**
     * 功能描述: 优先购权益核销
     * @Author: LSR
     * @Date: 2023/6/19 11:47
     */
    @EventListener(PaySuccessOrderEvent.class)
    @Order(PaySuccessOrderOrder.RIGHTS)
    public void updatePersonRights(PaySuccessOrderEvent event) {
        log.info("优先购资格核销================================");
        try{
            for(com.yami.shop.bean.model.Order order : event.getOrders()){
                for(OrderItem orderItem : order.getOrderItems()){
                    if(ObjectUtil.isNotEmpty(orderItem.getQualificationId()) && orderItem.getQualificationId() != 0){
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("qualificationId",orderItem.getQualificationId());
                        jsonObject.put("userId",orderItem.getUserId());
                        jsonObject.put("orderItemId",orderItem.getOrderItemId());
                        jsonObject.put("type","9");
                        personRightsFactory.verificationRights(jsonObject);
                    }
                }
            }
        }catch (Exception e){
            log.info("核销权益异常===={}",e);
        }
    }

}
